package com.cn.shine.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cn.shine.common.utils.DateUtil;
import com.cn.shine.dao.OrderMapper;
import com.cn.shine.pojo.Customer;
import com.cn.shine.pojo.Order;
import com.cn.shine.service.CustomerService;
import com.cn.shine.service.OrderService;

@Service("orderService")  
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private CustomerService customerService;
    
    @Override
    public int deleteByPrimaryKey(Integer id) {

        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int addOrder(Order record) {
        Customer c = customerService.selectByPrimaryKey(record.getCustomerId());
        if(c==null){
            return 0;
        }
        Customer cc = new Customer();
        cc.setId(c.getId());
        if(c.getMoney()<record.getTotalMoney()){
        	record.setMoneyAfter(0);
        	cc.setMoney(0);
        }else{
        	record.setMoneyAfter(record.getMoneyBefore()-record.getTotalMoney());
        	cc.setMoney(c.getMoney()-record.getTotalMoney());
        }
        customerService.updateByPrimaryKeySelective(cc);
        record.setPayDate(DateUtil.doGetDateOrTime("yyyy-MM-dd HH:mm:ss"));
        return orderMapper.insertSelective(record);
    }


    @Override
    public Order selectByPrimaryKey(Integer id) {

        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {

        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        
        return orderMapper.updateByPrimaryKey(record);
    }


	public List<Order> getOrderListByPage(Order record) {
		// TODO Auto-generated method stub
		return orderMapper.getOrderListByPage(record);
	}


	public int getOrderCountByPage(Order record) {
		// TODO Auto-generated method stub
		return orderMapper.getOrderCountByPage(record);
	}

    
    public List<Order> getPayTypeRank() {

        return orderMapper.getPayTypeRank();
    }

    @Override
    public List<Order> getCustRank() {
 
        return orderMapper.getCustRank();
    }

    @Override
    public String getAvgPay() {
        return orderMapper.getAvgPay();
    }

	@Override
	public List<Order> getMonthPayByYear(String year) {
		List<Order> list=orderMapper.getMonthPayByYear(year);
		for(Order o:list){
			String month=o.getMonth();
			if(month.startsWith("0")){
				month = month.substring(1,month.length());
			}
			month = month+"月";
			o.setMonth(month);
		}
		return list;
	}

}
